package com.example.itshome.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharePreferencesUtil {
        public final static String USERFile="userinfo";
        private final String preferencesName;
        private final Context context;

        public SharePreferencesUtil(Context context,String preferencesName){
            this.context=context;
            this.preferencesName=preferencesName;
        }
        /**存储字符串*/
        public boolean putString(String key, String value) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, value);
            return editor.commit();
        }
        /**读取字符串*/
        public String getString(String key) {
            return getString(key, null);
        }
        /**读取字符串（带默认值的）*/
        public String getString(String key, String defaultValue) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            return preferences.getString(key, defaultValue);
        }
        /**存储整型数字*/
        public boolean putInt(String key, int value) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(key, value);
            return editor.commit();
        }
        /**读取整型数字*/
        public int getInt(String key) {
            return getInt(key, -1);
        }
        /**读取整型数字（带默认值的）*/
        public int getInt(String key, int defaultValue) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            return preferences.getInt(key, defaultValue);
        }
        /**存储长整型数字*/
        public boolean putLong(String key, long value) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putLong(key, value);
            return editor.commit();
        }
        /**读取长整型数字*/
        public long getLong(String key) {
            return getLong(key, -1);
        }
        /**读取长整型数字（带默认值的）*/
        public long getLong(String key, long defaultValue) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            return preferences.getLong(key, defaultValue);
        }
        /**存储Float数字*/
        public boolean putFloat(String key, float value) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putFloat(key, value);
            return editor.commit();
        }
        /**读取Float数字*/
        public float getFloat(String key) {
            return getFloat(key, -1.0f);
        }
        /**读取Float数字（带默认值的）*/
        public float getFloat(String key, float defaultValue) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            return preferences.getFloat(key, defaultValue);
        }
        /**存储boolean类型数据*/
        public boolean putBoolean(String key, boolean value) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(key, value);
            return editor.commit();
        }
        /**读取boolean类型数据*/
        public boolean getBoolean(String key) {
            return getBoolean(key, false);
        }
        /**读取boolean类型数据（带默认值的）*/
        public boolean getBoolean(String key, boolean defaultValue) {
            SharedPreferences preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            return preferences.getBoolean(key, defaultValue);
        }
        /**清除数据*/
        public boolean clearPreferences() {
            SharedPreferences pref = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.clear();
            return editor.commit();
        }


}
